package edu.henu.personnel.dao;

import edu.henu.personnel.entity.Talent;
import edu.henu.personnel.entity.Teacher;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @program: PersonnelProject
 * @description: 人才列表Dao
 * @author: zhuyang
 */

@Repository
public interface TalentDao {

    /**
     * 通过 tid 获取其所对应的人才类别
     * @param tid
     * @return 返回其所对应的人才类别信息
     */
    @Select("select * from t_talent where ttid = #{ttid}")
    Talent getTalentInfoById(@Param("ttid") Integer ttid);


    @Select("select tname from t_talent where ttid = #{ttid}")
    String getTalentNameById(@Param("ttid") Integer ttid);

    /**
     * 通过 tname 获取其所对应的人才类别
     * @param tname
     * @return 返回相应的人才人才类别信息
     */
    @Select("select * from t_talent where tname = #{tname}")
    Talent getTalentInfoByTname(@Param("tname") String tname);

    /**
     * 获取所有的人才分类列表
     * @return
     */
    @Select("select * from t_talent")
    List<Talent> getAllTalentList();

    /**
     * 通过制定的ttid获取其所在分类所有的优秀人才教师
     * @param ttid
     * @return 返回Talent，其中专门封装的外联属性teachers，可以得到其所在分类所有的优秀人才教师
     */
    @Select("select * from t_talent where ttid = #{ttid}")
    @Results({
            @Result(id = true, property = "ttid", column = "ttid"),
            @Result(property = "tname", column = "tname"),
            @Result(property = "tmemo", column = "tmemo"),
            @Result(property = "teachers", column = "ttid", javaType = List.class,
                    many = @Many(select = "edu.henu.personnel.dao.TeacherDao.getTeacherListByTtid", fetchType = FetchType.LAZY))
    })
    Talent getAllTeacherInTheTalentById(@Param("ttid") Integer ttid);

    /**
     * 插入人才类别信息
     * @return 插入成功返回true，否则返回false
     */
    @Insert("insert into t_talent (tname, tmemo) " +
            "values " +
            "(#{tname}, #{tmemo})")
    @Options(useGeneratedKeys=true, keyProperty="ttid")
    int insertTalentInfo(Talent talent);


    /**
     * 修改人才类别信息
     * @param talent
     * @return 修改成功返回true，否则返回false
     */
    @Update("update t_talent set tname = #{tname}, " +
            "tmemo = #{tmemo} " +
            "where ttid = #{ttid}")
    int updateTalentInfo(Talent talent);

    /**
     * 删除人才类别信息
     * @param ttid
     * @return 删除成功返回true，否则返回false
     */
    @Delete("delete from t_talent where ttid = #{ttid}")
    int deleteTalentInfo(@Param("ttid") Integer ttid);

}
